#pragma once

#include <string>
#include <iostream>
#include<jsoncpp/json/json.h>
#include<memory>
#include<sstream>
using namespace std;

const string Sep = "\n"; //报文分割符


bool EnCode(string& message)
{
     if(message.size() == 0) return false; //报文为空就不发,不添加报头
     string package = to_string(message.size()) + Sep + message + Sep; //报头 报文
     message = package;     
     return true;
}

// len\n(json串)\n 
bool Decode(string& package,string* content)
{
   //协议规定:从前往后遇到的第一个分割符,前面一定是数字
   auto pos = package.find(Sep);
   if(pos == string::npos) return false; //解析失败没有读到完整报文
   string content_length_str = package.substr(0,pos);
   int content_length = stoi(content_length_str) ; //有效载荷的长度
   //123\r\n{json} 比如这种不完整的不符合我们要求->不能被解析
   int full_length = content_length_str.size() + content_length + 2* Sep.size(); //packge要解析成功应该的长度
   if(package.size() < full_length) 
      return false;
   
   *content = package.substr(pos+Sep.size(),content_length); //json串
   //package erase
   package.erase(0,full_length); //可能是多个报文，移除掉我们提取的一个完整报文 比如123\n\r{json}\r\n23
   return true; 

}

// _x oper _y
class Request
{
public:
    Request(int x, int y, char oper)
        : _x(x), _y(y), _oper(oper)
    {
    }
    Request()
    :_x(0),_y(0),_oper(0)
    {}
    ~Request() {}

    bool Serializze(string& out_string) // 序列化
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        Json::StreamWriterBuilder wb;
        unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        stringstream ss;
        w->write(root,&ss);
        out_string = ss.str();

        return true;
    }

    bool Deserializze(string& in_string) // 反序列化
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string,root);
        if(!parsingSuccessful)
        {
            cout << "Failed to parse Json: " << reader.getFormatedErrorMessages()<< endl;
            return false;
        }

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt(); ///char类型就是整数

        return true;   
    }

    void Print()
    {
        cout << "X :" << _x << endl;
        cout << "Y :" << _y << endl;
        cout << "oper :" << _oper << endl;
    }
    int X() const {return _x;}
    int Y() const {return _y;}
    char Oper() const {return _oper;}

private:
    int _x;
    int _y;
    char _oper;
};

// 协议体现：约定_result表示结果 _code表示出错吗
class Response
{
public:
    Response(int result, int code)
        : _result(result), _code(code)
    {}
    Response()
    :_result(0),_code(0)
    {}
    ~Response() {}
  
    bool Serializze(string& out_string) // 序列化
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        Json::StreamWriterBuilder wb;
        unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        stringstream ss;
        w->write(root,&ss);
        out_string = ss.str();

        return true;
    }

  
    bool Deserializze(string& in_string) // 反序列化
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string,root);
        if(!parsingSuccessful)
        {
            cout << "Failed to parse Json: " << reader.getFormatedErrorMessages()<< endl;
            return false;
        }

        _result = root["result"].asInt();
        _code = root["code"].asInt();

        return true;   
    }

    int Result() const{return _result;}
    int Code()const {return _code;}
    void SetResult(int res)
    {
        _result = res;
    }

    void SetCode(int code)
    {
        _code = code;
    }

private:
    int _result; // 结果
    int _code;   // 出错码：约定0，1，2，3表示错误
};
